package com.gzyct.m.api.common.sign;

import java.util.HashMap;
import java.util.Map;

import com.gzyct.m.api.common.biz.req.BizRequest;
import com.gzyct.m.api.common.intf.req.InterfaceRequest;
import com.gzyct.m.api.common.intf.resp.InterfaceResp;

public class SignWorker {

	private static Map<SignType, Signer> map = new HashMap<SignType, Signer>();

	static {
		map.put(SignType.MD5, new MD5Signer());
		map.put(SignType.HmacSHA256, new HmacSigner());
	}

	public static boolean verifySign(InterfaceRequest ir) throws Exception {
		SignType signType = SignType.valueOf(ir.getSign_type());
		if (signType == null) {
			throw new RuntimeException("No sign type found for type " + ir.getSign_type());
		}
		Signer signer = map.get(signType);
		if (signer == null) {
			throw new RuntimeException("No signer found for sign type " + signType);
		} else {
			return signer.verifySign(ir);
		}
	}

	public static InterfaceResp sign(InterfaceResp ir) throws Exception {
		SignType signType = SignType.valueOf(ir.getSign_type());
		if (signType == null) {
			throw new RuntimeException("No sign type found for type " + ir.getSign_type());
		}
		Signer signer = map.get(signType);
		if (signer == null) {
			throw new RuntimeException("No signer found for sign type " + signType);
		} else {
			return signer.sign(ir);
		}
	}

	public static BizRequest sign(BizRequest req) throws Exception {
		SignType signType = SignType.valueOf(req.getSign_type());
		if (signType == null) {
			throw new RuntimeException("No sign type found for type " + req.getSign_type());
		}
		Signer signer = map.get(signType);
		if (signer == null) {
			throw new RuntimeException("No signer found for sign type " + signType);
		} else {
			return signer.sign(req);
		}
	}

	public static BizRequest sign(BizRequest req, SignType signType) throws Exception {
		Signer signer = map.get(signType);
		if (signer == null) {
			throw new RuntimeException("No signer found for sign type " + signType);
		} else {
			req.setSign_type(signType.toString());
			return signer.sign(req);
		}
	}

	public static BizRequest signMD5(BizRequest req) throws Exception {
		return sign(req, SignType.MD5);
	}

	public static BizRequest sign(BizRequest req, String signKey) throws Exception {
		SignType signType = SignType.valueOf(req.getSign_type());
		if (signType == null) {
			throw new RuntimeException("No sign type found for type " + req.getSign_type());
		}
		Signer signer = map.get(signType);
		if (signer == null) {
			throw new RuntimeException("No signer found for sign type " + signType);
		} else {
			return signer.sign(req, signKey);
		}
	}

	public static BizRequest sign(BizRequest req, SignType signType, String signKey) throws Exception {
		Signer signer = map.get(signType);
		if (signer == null) {
			throw new RuntimeException("No signer found for sign type " + signType);
		} else {
			req.setSign_type(signType.toString());
			return signer.sign(req, signKey);
		}
	}

	public static BizRequest signMD5(BizRequest req, String signKey) throws Exception {
		return sign(req, SignType.MD5, signKey);
	}

	public static String signString(Object obj) throws Exception {
		return AbstractSigner.signString(obj);
	}

}
